import BaseUI from "../BaseUI";
import { Quicklist_event } from "./QuickLIst";

///////////// 快捷列表 item 组件
const { ccclass, property } = cc._decorator;

@ccclass
export default class QuickList_item extends BaseUI {
    init_data(d: object): void {

    }
    baseUI_onLoad(): void { }
    baseUI_onDestroy(): void { }
    onBtn_click(target: cc.Button, onBtn_name: string): number | void {
        if (this.item_button_event) {
            return this.item_button_event.button_click(this, this.data, onBtn_name, target, this.index);
        }
    }
    start() {

    }

    item_button_event: Quicklist_event<any> = null;
    data: any = null;
    index: number = 0;
    set_data(d: any, qle: Quicklist_event<any>, index: number) {
        this.data = d;
        this.item_button_event = qle;
        this.index = index;

        if (qle.is_auto_label == true) {
            this.auto_label();
        }

        if (qle.is_auto_urlSprite == true) {
            this.auto_urlSprite();
        }
    }

    /*****************************************************
     *              自动 填入数据
     *****************************************************/
    labs: cc.Label[];
    auto_label() {
        this.labs = this.node.getComponentsInChildren(cc.Label);

        if (typeof this.data == "object") {
            this.auto_label_bianli(this.data);
        }
    }

    auto_label_pipei(key: string, value: string) {
        let t = this.labs.find((v: cc.Label) => {
            return v.node.name == key || v.node.name == "lab_" + key;
        })
        if (t) {
            t.string = value + "";
        }
    }

    /////////// 遍历数据 匹配 lable
    auto_label_bianli(obj: Object) {
        for (const key in obj) {
            let type = typeof obj[key];
            if (type == "string" || type == "number") {
                this.auto_label_pipei(key, obj[key])
            } else if (type == 'object' && obj[key] != null) {
                if (!Array.isArray(obj[key])) { ////////// 子对象
                    this.auto_label_bianli(obj[key] as object)
                }
            }
        }
    }

    sprs: cc.Sprite[];
    auto_urlSprite() {
        this.sprs = this.node.getComponentsInChildren(cc.Sprite);

        if (typeof this.data == "object") {
            this.auto_urlSprite_bianli(this.data);
        }
    }


    auto_urlSprite_pipei(key: string, value: string) {
        if (!this.isValidURL(value)) {
            return;
        }
        let t = this.sprs.find((v: cc.Sprite) => {
            return v.node.name == key || v.node.name == "spr_" + key || v.node.name == "url_" + key;
        })
        if (t) {
            this.SetUrlSprite(t.node, value);
        }
    }

    /////////// 遍历数据 匹配 lable
    auto_urlSprite_bianli(obj: Object) {
        for (const key in obj) {
            let type = typeof obj[key];
            if (type == "string") {
                this.auto_urlSprite_pipei(key, obj[key])
            } else if (type == 'object' && obj[key] != null) {
                if (!Array.isArray(obj[key])) { ////////// 子对象
                    this.auto_urlSprite_bianli(obj[key] as object)
                }
            }
        }
    }

    isValidURL(s: string): boolean {
        // 使用正则表达式匹配常见的网址格式  
        // 注意：这个正则表达式并不完美，但足以覆盖大多数情况  
        const urlPattern = /^(?:(?:https?|ftp):\/\/)?(?:\S+(?::\S*)?@)?(?:[a-zA-Z0-9-]+(?::[a-zA-Z0-9-]+)*\.)+(?:[a-zA-Z]{2,6}|[a-zA-Z0-9-]{2,})|localhost|\d{1,3}(?:\.\d{1,3}){3}(?::\d+)?(?:\/[^\s]*)?$/;  
      
        return urlPattern.test(s);
    }


}